package com.beiming.libra.service.impl;

import com.beiming.libra.common.enums.ArbitrationCaseStatusEnum;
import com.beiming.libra.common.utils.CommonsUtils;
import com.beiming.libra.dao.ArbitrationCaseAdjustMapper;
import com.beiming.libra.dao.ArbitrationCaseAdjustMessagesMapper;
import com.beiming.libra.dao.ArbitrationCaseMapper;
import com.beiming.libra.domain.ArbitrationCase;
import com.beiming.libra.domain.ArbitrationCaseAdjust;
import com.beiming.libra.domain.ArbitrationCaseAdjustMessages;
import com.beiming.libra.domain.UserMessage;
import com.beiming.libra.domain.dto.responsedto.CaseAdjustResponseDTO;
import com.beiming.libra.domain.dto.responsedto.UserResponseDTO;
import com.beiming.libra.service.ArbitrationCaseAdjustService;
import com.beiming.libra.service.UserMessageService;
import com.beiming.libra.service.UserService;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

/**
 * @Auther: tyrion
 * @Date: 2018/6/7 10:55
 * @类描述:调解申请与撤案Impl
 */
@Service
public class ArbitrationCaseAdjustServiceImpl implements ArbitrationCaseAdjustService {

  @Autowired
  ArbitrationCaseAdjustMapper arbitrationCaseAdjustMapper;

  @Autowired
  UserMessageService userMessageService;

  @Autowired
  ArbitrationCaseMapper arbitrationCaseMapper;

  @Autowired
  ArbitrationCaseAdjustMessagesMapper arbitrationCaseAdjustMessagesMapper;

  @Autowired
  private UserService userService;

  public void insertOrUpdateStatus(ArbitrationCaseAdjust aca) {
    /**id为空，为新增调解信息**/
    if (StringUtils.isBlank(aca.getId())) {
      aca.setId(CommonsUtils.get32BitUUID());
      aca.setApplyStatus("0"); //可不加
      arbitrationCaseAdjustMapper.insertAdjust(aca);
    } else {
      aca.setUpdateTime(new Date());
//      aca.setUpdateUser();
      arbitrationCaseAdjustMapper.updateAdjust(aca);
    }
  }

  public List<CaseAdjustResponseDTO> listAllParties(String caseId) {
    return arbitrationCaseAdjustMapper.listAllParties(caseId);
  }

  @Transactional(rollbackFor = Exception.class)
  public Boolean notifySecreIfAllSend(String caseId) {
    //TODO 先查询是否所有都发送了申请
    List<CaseAdjustResponseDTO> listAllParties = arbitrationCaseAdjustMapper
        .listAllParties(caseId);
    Boolean flag = true;
    for (CaseAdjustResponseDTO ca : listAllParties) {
      if ("0".equals(ca.getApplyStatus())) {
        flag = false;
        break;
      }
    }

    if (flag) {
      //拿到案件信息
      ArbitrationCase ac = getCaseInfo(caseId);
      String secretaryId = ac.getSecretaryId();
      String caseName = ac.getCaseName();
      String caseNo = ac.getCaseNo();
      UserMessage um = new UserMessage();
      um.setMessageTitle("案件：" + caseName + "请求调解！");
      um.setMessageContent("案件：" + caseName + "编号：" + caseNo + "所有当事人均申请调解，请处理");
      um.setReceiverId(secretaryId);
      um.setCaseId(caseId);
      um.setMessageType("2");
      userMessageService.insertUserMessage(um);

      ArbitrationCase ac1 = new ArbitrationCase();
      ac1.setId(caseId);
      ac1.setCaseStatus(ArbitrationCaseStatusEnum.ADJUST_APP.getCode());
      arbitrationCaseMapper.updateByPrimaryKeySelective(ac1);
      return true;
    } else {
      return false;
    }


  }


  //拿到办案信息
  private ArbitrationCase getCaseInfo(String caseId) {
    Example arbitrationCaseExample = new Example(ArbitrationCase.class, false);
    arbitrationCaseExample.createCriteria().andEqualTo("id", caseId);
    ArbitrationCase ac = arbitrationCaseMapper.selectOneByExample(arbitrationCaseExample);
    if (ac != null) {
      return ac;
    }
    return null;
  }

  public List<ArbitrationCaseAdjustMessages> listLeaveMessages(String caseId) {
    return arbitrationCaseAdjustMessagesMapper.listLeaveMessages(caseId);
  }

  public void insertLeaveMessage(ArbitrationCaseAdjustMessages acam) {
    acam.setId(CommonsUtils.get32BitUUID());
    //当前留言人的信息
    String userId = acam.getId();
    if (userId != null) {
      UserResponseDTO user = userService.getUserById(acam.getUserId());
      String userName = user == null ? "" : user.getUser().getLoginName();
      acam.setUserId(userId);
      acam.setUserName(userName);
    }
    arbitrationCaseAdjustMessagesMapper.insertLeaveMessage(acam);
  }

}
